package com.gitee.wsl.compose.mvi.ui.progress.indicator

import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Stable
import androidx.compose.ui.Modifier
import com.gitee.wsl.compose.mvi.component.Projector


class ProgressIndicatorProjector<Presenter>(
    interactor: ProgressIndicatorInteractor = ProgressIndicatorInteractor.Default,
    presenter: Presenter
) : Projector<ProgressIndicatorInteractor, Presenter>(
    interactor, presenter
) where Presenter : ProgressIndicatorPresenter<Presenter> {
    companion object {
        @Stable
        val IndeterminateCircular = ProgressIndicatorProjector(
            presenter = CircularIndicatorPresenter.Default
        )

        @Stable
        val IndeterminateLinear = ProgressIndicatorProjector(
            presenter = LinearIndicatorPresenter.Default
        )
    }


    @Composable
    override fun body(
        combinedModifier: Modifier,
        interactor: ProgressIndicatorInteractor,
        presenter: Presenter,
    ) {
        if (interactor.progress == null) {
            // Display the indefinite progress indicator.
            when (presenter) {
                is CircularIndicatorPresenter -> CircularProgressIndicator(
                    modifier = combinedModifier,
                    color = presenter.color,
                    strokeWidth = presenter.strokeWidth
                )
                is LinearIndicatorPresenter -> LinearProgressIndicator(
                    modifier = combinedModifier,
                    color = presenter.color,
                    trackColor = presenter.backgroundColor
                )
            }
        } else {
            // Display the progress indicator with explicitly defined progress.
            when (presenter) {
                is CircularIndicatorPresenter -> CircularProgressIndicator(
                    progress = { interactor.progress },
                    modifier = combinedModifier,
                    color = presenter.color,
                    strokeWidth = presenter.strokeWidth,
                )
                is LinearIndicatorPresenter -> LinearProgressIndicator(
                    progress = { interactor.progress },
                    modifier = combinedModifier,
                    color = presenter.color,
                    trackColor = presenter.backgroundColor,
                )
            }
        }
    }
}